const syncRequest = require('sync-request');
const axios = require('axios');

/* syncRequest 同步 请求 */
module.exports.getDoc = () => {
  const queryUrl = 'http://hq.sinajs.cn?list=sh601872';
  const res = syncRequest('GET', queryUrl, {
    headers: {
      // "content-type": 'application/x-www-form-urlencoded; charset=UTF-8',
      'content-type': 'application/json; charset=UTF-8',
      referer: 'https://finance.sina.com.cn',
    },
  });
  let result;
  if (res && res.statusCode == 200) {
    let body = res.getBody();
    // let jsonObj = JSON.parse(body); // 解析接口返回的JSON内容
    let jsonObj = body.toString(); // 返回的数据是 Buffer
    if (jsonObj) {
      // 数据处理
      result = jsonObj;
    }
  }
  return result;
};

/* syncRequest 同步请求 tenxun */
module.exports.getAsyncTx = (codeId = 'sh000001') => {
	const url = 'http://qt.gtimg.cn/q=' + codeId
  const stocksData = syncRequest('GET', url, {
    headers: {
      // "content-type": 'application/x-www-form-urlencoded; charset=UTF-8',
      'content-type': 'application/json; charset=UTF-8',
      // referer: 'https://finance.sina.com.cn',
    },
  });
  let result;
  if (stocksData && stocksData.statusCode == 200) {
    let body = stocksData.getBody();
    // let jsonObj = JSON.parse(body); // 解析接口返回的JSON内容
    let jsonObj = body.toString(); // 返回的数据是 Buffer
    if (jsonObj) { // 装换后的字符串
      // 数据处理
      // result = jsonObj;
			const remove = jsonObj.split(/\n/g)[0]
			const start = remove.split('=')
			const end = start[1].split('~')
			result = { completeData: jsonObj, filterData: end }
    }
  }
  return result;
};

const requestAxios = async() => {
  const qUrl = 'http://hq.sinajs.cn?list=sh601872';
  const axiosR = await axios({
    method: 'get',
    url: qUrl,
    headers: {
      'content-type': 'application/json; charset=UTF-8',
      referer: 'https://finance.sina.com.cn',
    },
  })
	// console.log('axiosR', axiosR)
	if (!axiosR.data) return;
	return axiosR.data
};
module.exports.requestAxios = requestAxios;
module.exports.rToString = () => {
	requestAxios()
	.then(res => {
		console.log(res, 'res')
		return res
	})
}

/**
 * 腾讯接口获取分时
 * @param codeId
 * @returns 
 */
const txRequestAxios = async(codeId = 'sh000001') => {
	const url = 'http://qt.gtimg.cn/q=' + codeId
	const stocksData = await axios({
		method: 'get',
		url,
		// 以下为解决中文乱码的主要代码
		responseType: 'blob',
		transformResponse: [function(data) {
			// console.log('data', data)
			return data
		}],
		headers: {
			// 'content-type': 'text/html; charset=UTF-8'
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		}
	})
	const {status, statusText, data} = stocksData;
	// console.log('腾讯股票接口 stocksData', stocksData);

	if (status === 200 && statusText === 'OK') {
		const remove = data.split(/\n/g)[0]
		// console.log('remove', remove)
		const start = remove.split('=')
		const end = start[1].split('~')
		return {
			completeData: data,
			filterData: start
		}
	}
}
module.exports.txRequestAxios = txRequestAxios;

exports.test = '123';